﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Super_Wall_Breaker.Sources.GameEngine;

namespace Super_Wall_Breaker.Sources.TeamElements.PlayersControls
{
    /// <summary>
    /// This class defines the keyboard controls.
    /// </summary>
    public class KeyboardControlsState : ControlsState
    {
        private Keys _rightKey;
        private Keys _leftKey;
        private static Keys _pauseKey;

        /// <summary>
        /// Set the pause key. The pause key is common for all players.
        /// </summary>
        public Keys PauseKey
        {
            get { return KeyboardControlsState._pauseKey; }
            set { KeyboardControlsState._pauseKey = value; }
        }

        /// <summary>
        /// Gets or sets the right key.
        /// </summary>
        /// <value>
        /// The right key.
        /// </value>
        public Keys RightKey
        {
            get { return this._rightKey; }
            set { this._rightKey = value; }
        }

        /// <summary>
        /// Gets or sets the left key.
        /// </summary>
        /// <value>
        /// The left key.
        /// </value>
        public Keys LeftKey
        {
            get { return this._leftKey; }
            set { this._leftKey = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="KeyboardControlsState"/> class.
        /// </summary>
        /// <param name="index">The index of the player.</param>
        public KeyboardControlsState(PlayerIndex index) : base(index)
        {
            KeyboardControlsState._pauseKey = GlobalConfiguration.DEFAULT_PAUSE_KEY;

            switch (this._playerIndex)
            {
                case PlayerIndex.One :
                    this._rightKey = GlobalConfiguration.DEFAULT_PLAYER1_RIGHT_KEY;
                    this._leftKey = GlobalConfiguration.DEFAULT_PLAYER1_LEFT_KEY;
                    break;
                case PlayerIndex.Two :
                    this._rightKey = GlobalConfiguration.DEFAULT_PLAYER2_RIGHT_KEY;
                    this._leftKey = GlobalConfiguration.DEFAULT_PLAYER2_LEFT_KEY;
                    break;
                case PlayerIndex.Three:
                    this._rightKey = GlobalConfiguration.DEFAULT_PLAYER3_RIGHT_KEY;
                    this._leftKey = GlobalConfiguration.DEFAULT_PLAYER3_LEFT_KEY;
                    break;
                case PlayerIndex.Four:
                    this._rightKey = GlobalConfiguration.DEFAULT_PLAYER4_RIGHT_KEY;
                    this._leftKey = GlobalConfiguration.DEFAULT_PLAYER4_LEFT_KEY;
                    break;
            }
        }

        /// <summary>
        /// This function returns the state of the keyboard.
        /// </summary>
        /// <returns>Controls.RIGHT if the player go to right, LEFT if the player go to left. PAUSE if the player press the pause key. NONE otherwise.</returns>
        public override Controls GetState()
        {
            Controls entry = Controls.NONE;
            KeyboardState ks = Keyboard.GetState();

            if (ks.IsKeyDown(KeyboardControlsState._pauseKey) && this._previousEntry != Controls.PAUSE)
            {
                entry = Controls.PAUSE;
            }
            else if (ks.IsKeyDown(this._rightKey) && ks.IsKeyUp(this._leftKey))
            {
                entry = Controls.RIGHT;
            }
            else if (ks.IsKeyDown(this._leftKey) && ks.IsKeyUp(this._rightKey))
            {
                entry = Controls.LEFT;
            }

            this._previousEntry = entry;
            return entry;
        }
    }
}
